package org.wu.framework.authorization.platform.infrastructure.persistence;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.wu.framework.authorization.platform.infrastructure.converter.SysUserRoleConverter;
import org.wu.framework.authorization.platform.infrastructure.entity.SysUserRoleDO;
import org.wu.framework.authorization.platform.domain.model.sys.user.role.SysUserRole;
import org.wu.framework.authorization.platform.domain.model.sys.user.role.SysUserRoleRepository;
import org.wu.framework.lazy.orm.core.persistence.reverse.lazy.ddd.DefaultDDDLazyInfrastructurePersistence;
import org.wu.framework.lazy.orm.database.lambda.stream.lambda.LazyLambdaStream;
import org.wu.framework.lazy.orm.database.lambda.stream.wrapper.LazyWrappers;
import org.wu.framework.lazy.orm.web.plus.AbstractLazyCrudRepository;
import org.wu.framework.web.response.Result;
import org.wu.framework.web.response.ResultFactory;

import java.util.List;
import java.util.stream.Collectors;

/**
 * describe 用户角色关联关系
 *
 * @author Jia wei Wu
 * @date 2023/05/17 11:19 晚上
 * @see DefaultDDDLazyInfrastructurePersistence
 **/
@Repository
public class SysUserRoleRepositoryImpl extends AbstractLazyCrudRepository<SysUserRoleDO, SysUserRole, Integer> implements SysUserRoleRepository {

    @Autowired
    LazyLambdaStream lazyLambdaStream;


    /**
     * describe 新增用户角色关联关系
     *
     * @param
     * @return
     * @exception/throws
     * @author Jia wei Wu
     * @date 2023/05/17 11:19 晚上
     **/

    @Override
    public Result<SysUserRole> story(SysUserRole sysUserRole) {
        SysUserRoleDO sysUserRoleDO = SysUserRoleConverter.fromUserRole(sysUserRole);
        lazyLambdaStream.upsert(sysUserRoleDO);
        return ResultFactory.successOf();
    }

    /**
     * describe 查询单个用户角色关联关系
     *
     * @param
     * @return
     * @exception/throws
     * @author Jia wei Wu
     * @date 2023/05/17 11:19 晚上
     **/

    @Override
    public Result<SysUserRole> findOne(SysUserRole sysUserRole) {
        SysUserRoleDO sysUserRoleDO = SysUserRoleConverter.fromUserRole(sysUserRole);
        SysUserRole sysUserRoleOne = lazyLambdaStream.selectOne(LazyWrappers.lambdaWrapperBean(sysUserRoleDO), SysUserRole.class);
        return ResultFactory.successOf(sysUserRoleOne);
    }

    /**
     * describe 查询多个用户角色关联关系
     *
     * @param
     * @return
     * @exception/throws
     * @author Jia wei Wu
     * @date 2023/05/17 11:19 晚上
     **/

    @Override
    public Result<List<SysUserRole>> findList(SysUserRole sysUserRole) {
        SysUserRoleDO sysUserRoleDO = SysUserRoleConverter.fromUserRole(sysUserRole);
        List<SysUserRole> sysUserRoleList = lazyLambdaStream.selectList(
                LazyWrappers.lambdaWrapperBean(sysUserRoleDO)
                .eq(SysUserRoleDO::getIsDeleted,false), SysUserRole.class);
        return ResultFactory.successOf(sysUserRoleList);
    }

    /**
     * describe 删除用户角色关联关系
     *
     * @param
     * @return
     * @exception/throws
     * @author Jia wei Wu
     * @date 2023/05/17 11:19 晚上
     **/

    @Override
    public Result<SysUserRole> delete(SysUserRole sysUserRole) {
        SysUserRoleDO sysUserRoleDO = SysUserRoleConverter.fromUserRole(sysUserRole);
        //  lazyLambdaStream.remove(sysUserRoleDO);
        return ResultFactory.successOf();
    }

    /**
     * description: 批量存储用户角色
     *
     * @param userId       用户ID
     * @param sysUserRoleList 用户角色数据
     * @return
     * @author 吴佳伟
     * @date: 21.5.23 22:42
     */
    @Override
    public void batchStoryUserRole(Long userId, List<SysUserRole> sysUserRoleList) {
        SysUserRoleDO sysUserRoleDO = new SysUserRoleDO();
        sysUserRoleDO.setUserId(userId);
        sysUserRoleDO.setIsDeleted(true);
        lazyLambdaStream.update(sysUserRoleDO, LazyWrappers.<SysUserRoleDO>lambdaWrapper()
                .eq(SysUserRoleDO::getUserId, userId)
        );
        List<SysUserRoleDO> sysUserRoleDOList = sysUserRoleList.stream().map(SysUserRoleConverter::fromUserRole).collect(Collectors.toList());
        lazyLambdaStream.upsert(sysUserRoleDOList);
    }
}